document.addEventListener('DOMContentLoaded', function() {
    // 轮播图功能
    const carousel = {
        slides: document.querySelectorAll('.slide'),
        indicators: document.querySelectorAll('.indicator'),
        prevBtn: document.querySelector('.carousel-btn.prev'),
        nextBtn: document.querySelector('.carousel-btn.next'),
        currentIndex: 0,
        interval: null,
        duration: 5000,

        init: function() {
            this.setupEventListeners();
            this.startAutoPlay();
        },

        setupEventListeners: function() {
            this.prevBtn.addEventListener('click', () => {
                this.stopAutoPlay();
                this.prevSlide();
                this.startAutoPlay();
            });

            this.nextBtn.addEventListener('click', () => {
                this.stopAutoPlay();
                this.nextSlide();
                this.startAutoPlay();
            });

            this.indicators.forEach(indicator => {
                indicator.addEventListener('click', () => {
                    const index = parseInt(indicator.getAttribute('data-index'));
                    this.stopAutoPlay();
                    this.goToSlide(index);
                    this.startAutoPlay();
                });
            });
        },

        startAutoPlay: function() {
            this.interval = setInterval(() => {
                this.nextSlide();
            }, this.duration);
        },

        stopAutoPlay: function() {
            clearInterval(this.interval);
        },

        prevSlide: function() {
            this.goToSlide((this.currentIndex - 1 + this.slides.length) % this.slides.length);
        },

        nextSlide: function() {
            this.goToSlide((this.currentIndex + 1) % this.slides.length);
        },

        goToSlide: function(index) {
            this.slides[this.currentIndex].classList.remove('active');
            this.indicators[this.currentIndex].classList.remove('active');
            
            this.currentIndex = index;
            
            this.slides[this.currentIndex].classList.add('active');
            this.indicators[this.currentIndex].classList.add('active');
        }
    };

    // 图片懒加载
    const lazyLoad = {
        images: document.querySelectorAll('img[loading="lazy"]'),
        observer: null,

        init: function() {
            if ('IntersectionObserver' in window) {
                this.observer = new IntersectionObserver((entries) => {
                    entries.forEach(entry => {
                        if (entry.isIntersecting) {
                            const img = entry.target;
                            img.src = img.getAttribute('data-src') || img.src;
                            img.removeAttribute('data-src');
                            this.observer.unobserve(img);
                        }
                    });
                }, {
                    rootMargin: '200px 0px'
                });

                this.images.forEach(img => {
                    if (img.hasAttribute('data-src')) {
                        this.observer.observe(img);
                    }
                });
            }
        }
    };

    // 平滑滚动
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function(e) {
            e.preventDefault();
            const target = document.querySelector(this.getAttribute('href'));
            if (target) {
                target.scrollIntoView({
                    behavior: 'smooth'
                });
            }
        });
    });

    // 初始化
    carousel.init();
    lazyLoad.init();
});